package com.banshuibao.bsb_android.push;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.banshuibao.bsb_android.MyApplication;
import com.banshuibao.bsb_android.activity.ApplyToExpertActivity;
import com.banshuibao.bsb_android.activity.BalanceActivity;
import com.banshuibao.bsb_android.activity.CashListActivity;
import com.banshuibao.bsb_android.activity.DealActivity;
import com.banshuibao.bsb_android.activity.LoginActivity;
import com.banshuibao.bsb_android.activity.RecorderListActivity;
import com.banshuibao.bsb_android.activity.SystemInformationActivity;
import com.banshuibao.bsb_android.activity.TabActivity;
import com.banshuibao.bsb_android.activity.TabTwoActivity;
import com.banshuibao.bsb_android.util.IntentManager;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyh on 2017/2/9.
 * <p>
 * 自定义推送动作类
 */

public class UmPushNotificationClickHandler extends UmengNotificationClickHandler {

    private static final String TYPE_MESSAGE_ACTIVITY = "1";//我的消息
    private static final String TYPE_NO_ACTIVITY = "2";//不跳转
    private static final String TYPE_MY_RECORD_ACTIVITY = "3";//我的录音列表（不通过）
    private static final String TYPE_O2O_TAX = "4";//专家一对一
    private static final String TYPE_ACCEPT_OFFER = "5";//提示有悬赏问题（专家）
    private static final String TYPE_ADOPT_OFFER = "6";//提示问题被采纳/未被采纳（专家）
    private static final String TYPE_MY_WALLET = "7";//语音被购买 跳 钱包（专家）
    private static final String TYPE_O2O_LIST = "8";//咨询清单 一对一/追问（用户）
    private static final String TYPE_ADOPT_ANSWER = "9";//有未采纳的悬赏问题（用户）
    private static final String TYPE_TRADING_WALLET = "10";//我的钱包--交易记录
    private static final String TYPE_WITHDRAWAL_WALLET = "11";//我的钱包-提现记录
    private static final String TYPE_NO_PASS_TAX = "12";//申请专家不通过状态
    private static final String TYPE_PASS_TAX = "13";//申请专家通过状态

    MyApplication myApplication;

    @Override
    public void dealWithCustomAction(Context context, UMessage uMessage) {
        super.dealWithCustomAction(context, uMessage);
        myApplication = MyApplication.applicationInstance;
        String custom = uMessage.custom;
        JSONObject jsonObject = null;
        try {
            if (!TextUtils.isEmpty(custom))
                jsonObject = new JSONObject(custom);
            if (checkAppStart(context, context.getPackageName())) {
                if (jsonObject != null)
                    //跳转界面
                    if (TYPE_MESSAGE_ACTIVITY.equals(jsonObject.getString("type"))) {//1  我的消息
                        finishActivity("activity.SystemInformationActivity");
                        Intent it = new Intent(context, SystemInformationActivity.class);
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(it);

                    } else if (TYPE_MY_RECORD_ACTIVITY.equals(jsonObject.getString("type"))) {//3 我的录音列表（不通过）
                        finishActivity("activity.RecorderListActivity");
                        Intent it = new Intent(context, RecorderListActivity.class);
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(it);
                    } else if (TYPE_O2O_TAX.equals(jsonObject.getString("type"))) {//4 专家一对一
                        finishActivity("activity.TabTwoActivity");
                        Intent intent = new Intent(context, TabTwoActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);

                    } else if (TYPE_ACCEPT_OFFER.equals(jsonObject.getString("type"))) {//5 提示有悬赏问题（专家）
                        finishActivity("activity.CommitAnswerActivity");
                        finishActivity("activity.TobeActivity");
                        IntentManager.goPushIfAnswerActivity(context);

                    } else if (TYPE_ADOPT_OFFER.equals(jsonObject.getString("type"))) {//6 提示问题被采纳/未被采纳（专家）
                        finishActivity("activity.TabTwoActivity");
                        Intent intent = new Intent(context, TabTwoActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("flag", "1");
                        context.startActivity(intent);

                    } else if (TYPE_MY_WALLET.equals(jsonObject.getString("type"))) {//7 语音被购买 跳 钱包（专家）
                        finishActivity("activity.BalanceActivity");
                        Intent it = new Intent(context, BalanceActivity.class);
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(it);

                    } else if (TYPE_O2O_LIST.equals(jsonObject.getString("type"))) {//8 咨询清单 一对一/追问（用户）
                        finishActivity("activity.TabActivity");
                        finishActivity("activity.OneOnOneActivity");
                        IntentManager.isPushJurisdiction(context);

                    } else if (TYPE_ADOPT_ANSWER.equals(jsonObject.getString("type"))) {//9 有未采纳的悬赏问题（用户）
                        finishActivity("activity.TabActivity");
                        Intent intent = new Intent(context, TabActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("flag", 1);
                        context.startActivity(intent);

                    } else if (TYPE_TRADING_WALLET.equals(jsonObject.getString("type"))) {//10 我的钱包--交易记录
                        finishActivity("activity.DealActivity");
                        Intent it = new Intent(context, DealActivity.class);
                        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(it);

                    } else if (TYPE_WITHDRAWAL_WALLET.equals(jsonObject.getString("type"))) {//11 我的钱包-提现记录
                        finishActivity("activity.CashListActivity");
                        Intent intent = new Intent(context, CashListActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);

                    } else if (TYPE_NO_PASS_TAX.equals(jsonObject.getString("type"))) {//12 申请专家未通过

                        finishActivity("activity.ApplyToExpertActivity");
                        Intent intent = new Intent(context, ApplyToExpertActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("flag", 2);
                        context.startActivity(intent);

                    } else if (TYPE_PASS_TAX.equals(jsonObject.getString("type"))) {//申请专家通过

                        Intent intent = new Intent(context, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);

                    } else if (TYPE_NO_ACTIVITY.equals(jsonObject.getString("type"))) {//2 不跳转

                    }
            } else {
                startApp(context);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void finishActivity(String activityName) {
        ArrayList<Activity> aal = myApplication.getAal();
        if (aal != null && aal.size() > 0)
            for (Activity activity : aal) {
                if (activity.getLocalClassName().equals(activityName)) {
                    activity.finish();
                }
            }
    }


    /**
     * 启动app
     **/
    private void startApp(Context context) {
        try {
            Intent it = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
            context.startActivity(it);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断app是否启动
     **/
    private boolean checkAppStart(Context context, String packageName) {
        ActivityManager activityManager =
                (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
        for (int i = 0; i < processInfos.size(); i++) {
            if (processInfos.get(i).processName.equals(packageName)) {
                return true;
            }
        }
        return false;
    }
}
